package com.ysg.utils;

import android.util.Log;
import com.ysg.base.BaseApplication;
import com.ysg.db.ChatCountDb;
import com.ysg.db.UserLatLngDb;
import com.ysg.db.UserSensitiveDb;
import com.ysg.http.data.entity.expression.ExpressionEntity;
import com.ysg.http.data.entity.home.HomeResult;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据库
 */
public class YDbUtils {
    private static YDbUtils instance;

    private YDbUtils(){
    }

    public static YDbUtils getInstance(){
        if (instance == null){
            synchronized (YDbUtils.class){
                if (instance == null) {
                    instance = new YDbUtils();
                }
            }
        }
        return instance;
    }

    /**
     * 初始化
     */
    public void init(){
        LitePal.initialize(BaseApplication.getContext());
    }

    /**
     * 首页地图：用户经纬度
     */
    public void saveUserLatLng(UserLatLngDb data){
        UserLatLngDb userLatLng = LitePal.where("loginUserId = ? and userId = ?", data.getLoginUserId(), data.getUserId()).findLast(UserLatLngDb.class);
        if (userLatLng != null){
            userLatLng.setCreateTime(System.currentTimeMillis());
            userLatLng.save();
        }else{
            data.setCreateTime(System.currentTimeMillis());
            data.save();
        }
    }

    public UserLatLngDb findOneUserLatLng(String loginUserId, String userId){
        long nowTime = System.currentTimeMillis() - (10 * 60 * 1000);//10分钟
        UserLatLngDb data = LitePal.where("loginUserId = ? and userId = ? and createTime >= ?", loginUserId, userId, ""+nowTime).findLast(UserLatLngDb.class);
        if (data != null){
            Log.i("用户经纬度", data.toString());
        }

//        List<UserLatLngDb> list = LitePal.findAll(UserLatLngDb.class);
//        if (YCollectionUtil.isNotEmpty(list)){
//            Log.i("用户经纬度总数", list.size()+"");
//        }
        return data;
    }

    /**
     * 用户发送敏感词记录：保存
     */
    public void saveUserSensitive(String input, String sensitives){
        UserSensitiveDb data = new UserSensitiveDb();
        data.setUserId(YSPUtils.getInstance().getUserId());
        data.setInput(input);
        data.setSensitives(sensitives);
        data.setCreateTime(System.currentTimeMillis());
        data.save();
    }

    /**
     * 用户发送敏感词记录: 查询
     */
    public List<UserSensitiveDb> findUserSensitive(String userId){
        List<UserSensitiveDb> list = LitePal.where("userId = ?", userId).find(UserSensitiveDb.class);
        if (YCollectionUtil.isNotEmpty(list)){
            return list;
        }
        return null;
    }

    /**
     * 用户发送敏感词记录: 删除
     */
    public void deleteUserSensitive(String userId){
       LitePal.deleteAll(UserSensitiveDb.class, "userId = ?", userId);
    }

    /**
     * 用户发送敏感词记录: 查询是否超过次数
     * 提示3次封号---弹窗提示。然后封号。--------------保存本地--男的封号3天，女的永久封号
     */
    public boolean isThanCountByUserSensitive(String userId){
        List<UserSensitiveDb> list = LitePal.where("userId = ?", userId).find(UserSensitiveDb.class);
        if (YCollectionUtil.isNotEmpty(list)){
            Log.e("", ""+ list.size());
            if (list.size() >= 3){
                return true;
            }
        }
        return false;
    }


    /**
     * 首页：切换账号数据
     */
    public void saveHomeTabAccount(List<HomeResult> list){
        if (YCollectionUtil.isNotEmpty(list)){
            LitePal.deleteAll(HomeResult.class, "");
            LitePal.saveAll(list);
        }
    }

    /**
     * 首页：切换账号数据
     */
    public List<HomeResult> findHomeTabAccount(){
        List<HomeResult> list = LitePal.where().find(HomeResult.class);
        if (YCollectionUtil.isNotEmpty(list)){
            return list;
        }
        return new ArrayList<>();
    }

    /**
     * 聊天次数
     */
    public void saveChatCount(){
        String userId = YSPUtils.getInstance().getUserId();
        ChatCountDb data = LitePal.where("userId=?", userId).findLast(ChatCountDb.class);
        if (data == null){
            data = new ChatCountDb();
            data.setCount(1L);
            data.setUserId(userId);
        }else{
            data.setCount(data.getCount() + 1L);
        }
        data.save();
    }

    /**
     * 聊天次数
     */
    public boolean isThanChatCount(){
        String userId = YSPUtils.getInstance().getUserId();
        ChatCountDb data = LitePal.where("userId=?", userId).findLast(ChatCountDb.class);
        if (data == null){
            return false;
        }else{
             return data.getCount() >= 10? true: false;
        }
    }


    /**
     * 自定义常用语
     */
    public boolean saveExpression(List<ExpressionEntity> list){
        if (YCollectionUtil.isNotEmpty(list)){
            return LitePal.saveAll(list);
        }
        return false;
    }

    /**
     * 自定义常用语
     */
    public List<ExpressionEntity> findExpression(){
        List<ExpressionEntity> list = LitePal.where().find(ExpressionEntity.class);
        if (YCollectionUtil.isNotEmpty(list)){
            return list;
        }
        return new ArrayList<>();
    }

    /**
     * 用户发送敏感词记录: 删除
     */
    public int deleteExpression(ExpressionEntity entity){
        return entity.delete();
    }




}
